package com.corpgovernment.costcenter.controller;

import com.corpgovernment.api.costcenter.model.GetDepartmentsRequest;
import com.corpgovernment.api.organization.dto.request.ProjectInfoDto;
import com.corpgovernment.api.organization.model.project.request.CollectProjectInfoRequest;
import com.corpgovernment.api.organization.model.project.request.GetProjectInfoRequest;
import com.corpgovernment.api.organization.model.project.request.ProjectInfoRequest;
import com.corpgovernment.api.organization.model.project.response.ProjectInfoResponse;
import com.corpgovernment.api.organization.vo.GetOrgTreeRsp;
import com.corpgovernment.api.supplier.valid.Base;
import com.corpgovernment.api.supplier.valid.Extra;
import com.corpgovernment.common.base.BaseRequestVO;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.costcenter.entity.db.MbOrgProjectInfo;
import com.corpgovernment.costcenter.service.IProjectInfoService;
import com.corpgovernment.costcenter.vo.*;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/projectinfo")
public class ProjectInfoController {

    @Autowired
    private IProjectInfoService iProjectInfoService;

    /**
     * 新增项目
     *
     * @param request
     * @return
     */
    @PostMapping("/insert")
    public JSONResult<Integer> insert(@RequestBody @Validated(Base.class) ProjectInfoVo request) {
        int result = iProjectInfoService.insert(request);
        return JSONResult.success(result);
    }

    /**
     * 删除项目
     *
     * @return
     */
    @PostMapping("/delete")
    public JSONResult delete(@RequestBody @Validated ProjectInfoDelVo request) {
        boolean result = iProjectInfoService.delete(request);
        return JSONResult.success(result);
    }

    /**
     * 分页查询
     *
     * @param request
     * @return
     */
    @PostMapping("/select")
    public JSONResult select(@RequestBody @Validated ProjectInfoPageVo request) {
        return JSONResult.success(iProjectInfoService.select(request));
    }

    /**
     * 查询全部项目
     *
     * @param request
     * @return
     */
    @PostMapping("/selectPage")
    public JSONResult<ProjectInfoResponse> selectPage(@RequestBody GetProjectInfoRequest request) throws Exception {
        return JSONResult.success(iProjectInfoService.selectPage(request));
    }

    /**
     * 获取部门结构
     *
     * @param request
     * @return
     */
    @PostMapping("/getDepartments")
    public JSONResult<List<GetOrgTreeRsp>> getDepartments(@RequestBody @Validated GetDepartmentsRequest request) {
        return new JSONResult<>(iProjectInfoService.getOrgTree(request));
    }

    /**
     * 修改项目
     *
     * @param request
     * @return
     */
    @PostMapping("/update")
    public JSONResult update(@RequestBody @Validated(Extra.class) ProjectInfoVo request) {
        boolean result = iProjectInfoService.update(request);
        return JSONResult.success(result);
    }

    /**
     * 异常成本中心（原来：统计成本中心+项目，现：只统计成本中心）
     *
     * @return
     */
    @PostMapping(value = "/selectCostAndProjectOrgId")
    public JSONResult<List<String>> selectCostAndProjectOrgId() {
        return JSONResult.success(iProjectInfoService.selectCostAndProjectOrgId());
    }

    /**
     * Excel上传
     *
     * @param file
     * @param requestVo
     * @return
     */
    @PostMapping(value = "/syncProjectInfoExcel")
    public JSONResult syncProjectInfoExcel(@RequestParam("file") MultipartFile file, ProjectInfoExcelVo requestVo) {
        return JSONResult.success(iProjectInfoService.syncProjectInfoExcel(file, requestVo));
    }

    @PostMapping(value = "/selectProjectInfoByCompanyId")
    public JSONResult<ProjectInfoResponse> selectProjectInfoByCompanyId(@RequestBody GetProjectInfoRequest request) {
        List<MbOrgProjectInfo> projectInfos = iProjectInfoService.selectProjectInfoByCompanyId(request.getCompanyId());
        if (CollectionUtils.isEmpty(projectInfos)) {
            return JSONResult.ok();
        }
        ProjectInfoResponse response = new ProjectInfoResponse();
        List<ProjectInfoResponse.ProjectInfoData> projectInfoDataList = new ArrayList<>();
        projectInfos.forEach(item -> {
            ProjectInfoResponse.ProjectInfoData projectInfoData = new ProjectInfoResponse.ProjectInfoData();
            projectInfoData.setId(item.getId());
            projectInfoData.setCompany(item.getCompany());
            projectInfoData.setCompanyId(item.getCompanyId());
            projectInfoData.setProjectCode(item.getProjectCode());
            projectInfoData.setProjectName(item.getProjectName());
            projectInfoData.setProjectLeader(item.getProjectLeader());
            projectInfoData.setValid(item.getValid());

            if (!CollectionUtils.isEmpty(item.getDepartmentList())) {
                List<ProjectInfoResponse.OrgInfo> orgInfoList = new ArrayList<>();
                item.getDepartmentList().forEach(i -> {
                    ProjectInfoResponse.OrgInfo orgInfo = new ProjectInfoResponse.OrgInfo();
                    orgInfo.setOrgId(i.getOrgId());
                    orgInfo.setOrgName(i.getOrgName());
                    orgInfo.setUid(i.getUid());
                    orgInfo.setUserName(i.getUserName());
                    orgInfoList.add(orgInfo);
                });
                projectInfoData.setOrgInfoList(orgInfoList);
            }

            projectInfoDataList.add(projectInfoData);

        });
        response.setList(projectInfoDataList);
        return JSONResult.success(response);
    }

    /**
     * 查询上传记录
     *
     * @param requestVo
     * @return
     */
    @PostMapping(value = "/selectUploadInfoPage")
    public JSONResult selectUploadInfoPage(@RequestBody ProjectUploadInfoPageRequestVo requestVo) {
        return JSONResult.success(iProjectInfoService.selectUploadInfoPage(requestVo));
    }

    /**
     * 查询错误日志
     *
     * @param requestVo
     * @return
     */
    @PostMapping(value = "/selectUploadDetailInfoPage")
    public JSONResult selectUploadDetailInfoPage(@RequestBody ProjectUploadInfoPageRequestVo requestVo) {
        return JSONResult.success(iProjectInfoService.selectUploadDetailInfoPage(requestVo));
    }

    /**
     * 下载错误文件
     *
     * @param requestVo
     * @param response
     */
    @PostMapping(value = "/downloadUploadInfo")
    public void downloadUploadInfo(@RequestBody ProjectUploadInfoDownloadRequestVo requestVo,
            HttpServletResponse response) {
        iProjectInfoService.downloadUploadInfo(requestVo.getTaskNo(), response);
    }

    /**
     * 收藏项目号
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/collectProjectInfo")
    public JSONResult<Boolean> collectProjectInfo(@RequestBody CollectProjectInfoRequest request) {
        return new JSONResult<>(iProjectInfoService.collectProjectInfo(request));
    }

    /**
     * 下载成本中心内容
     */
    @PostMapping(value = "/downloadProjectInfo")
    public void downloadProjectInfo(@RequestBody CollectProjectInfoRequest request, HttpServletResponse response) {
        iProjectInfoService.downloadProjectInfo(request.getCompanyId(), response);
    }

    /**
     * 查询组织下绑定了哪些项目号
     *
     * @param orgId
     * @return
     */
    @PostMapping(value = "/getProjectInfoByOrgId")
    public JSONResult<List<ProjectInfoDto>> getProjectInfoByOrgId(@RequestParam("orgId") String orgId) {
        return new JSONResult<>(iProjectInfoService.getProjectInfoByOrgId(orgId));
    }

    /**
     * 查询组织下绑定了哪些项目号
     *
     * @param orgIdList
     * @return
     */
    @PostMapping(value = "/listProjectInfoByOrgId")
    public JSONResult<List<ProjectInfoDto>> listProjectInfoByOrgId(@RequestParam("orgIdList") List<String> orgIdList) {
        return new JSONResult<>(iProjectInfoService.listProjectInfoByOrgId(orgIdList));
    }
    /**
     * 成本中心同步
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/syncSaveProjectInfo")
    public JSONResult<Integer> syncSaveProjectinfo(@RequestBody ProjectInfoRequest request) {
        ProjectInfoVo projectInfoVo = new ProjectInfoVo();
        BeanUtils.copyProperties(request, projectInfoVo);
        BaseRequestVO.UserInfo userInfo = new BaseRequestVO.UserInfo();
        userInfo.setUid(request.getOperatorUid());
        userInfo.setUsername(request.getOperatorName());
        projectInfoVo.setUserInfo(userInfo);
        return insert(projectInfoVo);
    }

    @RequestMapping(value = "/getProjectByProjectCode")
    public JSONResult<List<ProjectInfoDto>> getProjectByProjectCode(@RequestParam("projectCodes") List<String> projectCodes) {
        return new JSONResult<>(iProjectInfoService.getProjectByProjectCode(projectCodes));
    }

    /**
     * 模糊查询项目编码及名称
     * 仅查询【项目名称/编码】
     * 返回当前 orgId 下全部项目名称与编码
     * 
     * @param request
     * @return
     */
    @PostMapping("/queryProjectNameCode")
    public JSONResult queryProjectNameCode(@RequestBody @Validated ProjectInfoPageVo request) {
        return JSONResult.success(iProjectInfoService.queryProjectNameCode(request));
    }

    /**
     * 查询组织下绑定了哪些项目号
     *
     * @param orgIdList
     * @return
     */
    @PostMapping(value = "/listProjectInfoByOrgIdNew")
    public JSONResult<Map<String, List<ProjectInfoDto>>> listProjectInfoByOrgIdNew(@RequestBody List<String> orgIdList) {
        return JSONResult.success(iProjectInfoService.listProjectInfoByOrgIdNew(orgIdList));
    }
}
